<template>
	<view class="message-time" v-if="timeShow">
		{{timestampShowContent}}
	</view>
</template>

<script>
	export default {
		props: {
			currTime: {
				type: Number,
				default: 0,
			},
			prevTime: {
				type: Number,
				default: 0,
			},
		},
		data() {
			return {
				timeShow: false,
				timestampShowContent: ''
			}
		},
		created() {
			try{
				this.timestampShowContent = this.handleItemTime(this.currTime, this.prevTime)
			}catch(e){
				console.log(e,'你犯事儿了哦！！！');
				//TODO handle the exception
			}
		},
		methods: {
			handleItemTime(currTime, prevTime) {
				this.timeShow = false;
				if (currTime <= 0) {
					return "";
				} else if (!prevTime || prevTime <= 0) {
					this.timeShow = true;
					return this.calculateTimestamp(currTime * 1000);
				} else {
					const minDiffToShow = 10 * 60; //10min 10*60s
					const diff = currTime - prevTime; //s
					if (diff >= minDiffToShow) {
						this.timeShow = true;
						return this.calculateTimestamp(currTime * 1000);
					}
				}
				return "";
			},
			calculateTimestamp(timestamp) {
				const todayZero = new Date().setHours(0, 0, 0, 0);
				const thisYear = new Date(
					new Date().getFullYear(),
					0,
					1,
					0,
					0,
					0,
					0
				).getTime();
				const target = new Date(timestamp);

				const oneDay = 24 * 60 * 60 * 1000;
				const oneWeek = 7 * oneDay;
				const oneYear = 365 * oneDay;

				const diff = todayZero - target.getTime();

				function formatNum(num) {
					return num < 10 ? "0" + num : num.toString();
				}

				if (diff <= 0) {
					// today, only display hour:minute
					return `${formatNum(target.getHours())}:${formatNum(target.getMinutes())}`;
				} else if (diff <= oneDay) {
					// yesterday, display yesterday:hour:minute
					return `${'昨天'} ${formatNum(
			      target.getHours()
			    )}:${formatNum(target.getMinutes())}`;
				} else if (diff <= oneWeek - oneDay) {
					// Within a week, display weekday hour:minute
					const weekdays = [
						"星期日",
						"星期一",
						"星期二",
						"星期三",
						"星期四",
						"星期五",
						"星期六",
					];
					const weekday = weekdays[target.getDay()];
					return `${TUITranslateService.t("time." + weekday)} ${formatNum(
			      target.getHours()
			    )}:${formatNum(target.getMinutes())}`;
				} else if (target.getTime() >= thisYear) {
					// Over a week, within this year, display mouth/day hour:minute
					return `${target.getMonth() + 1}/${target.getDate()} ${formatNum(
			      target.getHours()
			    )}:${formatNum(target.getMinutes())}`;
				} else {
					// Not within this year, display year/mouth/day hour:minute
					return `${target.getFullYear()}/${
			      target.getMonth() + 1
			    }/${target.getDate()} ${formatNum(target.getHours())}:${formatNum(
			      target.getMinutes()
			    )}`;
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.message-time {
		text-align: center;
		padding-bottom: 20rpx;
	}
</style>